<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @package phpbb3 :: Game Console Mod
* @version $Id: index.php 1.0.2 12-10-2009 Blackpawn $
* @copyright (c) 2004-2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/

// Report all errors, except notices
error_reporting(E_ALL ^ E_NOTICE);

define('GC_MOD', true);
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();


$user->add_lang('mods/game_console');

// Only founders can access this
if ($user->data['user_type'] != USER_FOUNDER)
{
	trigger_error($user->lang['UNAUTHORIZED_ACCESS'],E_USER_ERROR);
}

// Define Variables
$installed = FALSE;
$support_link = Null;
$major = Null;
$minor = Null;
$build = Null;
$installing_version = Null;
$version_installed = Null;
$error_msgs = Null;
$sql_data = Null;
$p_title = Null;
$gid = Null;
$error = FALSE;

// Purge Cache
$cache->purge();
					
// Var Requests
$cmd = request_var('cmd','');

// Game Console Data
$support_link = 'http://blackpawnsarchives.net';
$major = 1;
$minor = 0;
$build = 2;
$installing_version = $major.'.'.$minor.'.'.$build;
$p_title = $user->lang['CONSOLE_TITLE'];

if(!empty($config['gc_version_major']) || !empty($config['gc_version_minor']) || !empty($config['gc_version_build']))
{
	// Check to see if the game console is already install
	$version_major = ($config['gc_version_major']) ? $config['gc_version_major'] : 0;
	$version_minor = ($config['gc_version_minor']) ? $config['gc_version_minor'] : 0;
	$version_build = ($config['gc_version_build']) ? $config['gc_version_build'] : 0;
}else{
	$version_major = 0;
	$version_minor = 0;
	$version_build = 0;
}

if($version_major > 0 || $version_minor  > 0 || $version_build > 0 )
{
	$installed = TRUE;
	$version_installed = $version_major.'.'.$version_minor.'.'.$version_build;
	
	if($installing_version == $version_installed){
		$installing_version = $user->lang['ALREADY_INSTALLED'];
	}
	
}else{
	$installed = FALSE;
	$version_installed = $user->lang['NOT_INSTALLED']; 
}

if($cmd == 'confirm_del')
{
	if (confirm_box(true))				
	{	
		include_once($phpbb_root_path . 'includes/mods/game_console/gc_common.' . $phpEx);

		$sql = "SELECT * 
		FROM " . $table_prefix . "gc_games
		ORDER BY id ASC";
		$result = $db->sql_query($sql);
	
		while ($row = $db->sql_fetchrow($result))
		{
			$gid = $row['id'];
			gc_remove_game($gid);		
		}	
		$db->sql_freeresult($result);
		
		$db->sql_query("DROP table " . $table_prefix . "gc_accounts");
		$db->sql_return_on_error(false);
		
		$db->sql_query("DROP table " . $table_prefix . "gc_config");
		$db->sql_return_on_error(false);
		
		$db->sql_query("DROP table " . $table_prefix . "gc_games");
		$db->sql_return_on_error(false);
	
		$db->sql_query("DELETE FROM " . $table_prefix . "acl_options WHERE auth_option = 'u_gc'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "acl_options WHERE auth_option = 'a_gc'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "acl_options WHERE auth_option = 'a_gc_a_config'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "acl_options WHERE auth_option = 'a_gc_d_config'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "acl_options WHERE auth_option = 'a_gc_set_config'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "acl_options WHERE auth_option = 'a_gc_sty_config'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "acl_options WHERE auth_option = 'a_gc_u_config'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "acl_options WHERE auth_option = 'a_gc_m_config'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "modules WHERE module_langname = 'GAME_CONSOLE'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "modules WHERE module_langname = 'GAME_CONSOLE_UPDATES'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "modules WHERE module_langname = 'GAME_CONSOLE_SETTINGS'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "modules WHERE module_langname = 'GAME_CONSOLE_STYLE'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "modules WHERE module_langname = 'GAME_CONSOLE_ADD'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "modules WHERE module_langname = 'GAME_CONSOLE_DELETE'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "modules WHERE module_langname = 'GAME_CONSOLE_MANAGE'");
		$db->sql_return_on_error(false);

		$db->sql_query("DELETE FROM " . $table_prefix . "config WHERE config_name = 'gc_version_major'");
		$db->sql_return_on_error(false);
		
		$db->sql_query("DELETE FROM " . $table_prefix . "config WHERE config_name = 'gc_version_minor'");
		$db->sql_return_on_error(false);
		
		$db->sql_query("DELETE FROM " . $table_prefix . "config WHERE config_name = 'gc_version_build'");
		$db->sql_return_on_error(false);
		

		trigger_error($user->lang['UNINSTALL_WAS_SUCCESSFUL']);	
	}else{
		// This will redirect the founder back to the install & uninstall page while clearing the confirm key
		redirect(append_sid("{$phpbb_root_path}gc/index.$phpEx"));
	}	
}


if($cmd == $user->lang['UNINSTALL'])
{	
	if($installed == FALSE)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NO_GAME_FOUND'] . '<br />';		
	}
	
	if ($user->data['user_type'] != USER_FOUNDER)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['UNAUTHORIZED_ACCESS'] . '<br />';
	}

	if(!$error)
	{

		$s_hidden_fields = build_hidden_fields(array(
			'cmd' => 'confirm_del',
			)
		);

		//Display confirm box
		confirm_box(false, $user->lang['CONFIRM_CONSOLE_UNINSTALL'], $s_hidden_fields);	
		
	}
}

if($cmd == 'confirm_install')
{
	if (confirm_box(true))				
	{		
		
		$sql_data = "DROP TABLE IF EXISTS `" . $table_prefix . "gc_accounts`;";
		$db->sql_query($sql_data);
		
		$sql_data = "
		CREATE TABLE IF NOT EXISTS `" . $table_prefix . "gc_accounts` (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `game_id` int(11) NOT NULL DEFAULT '0',
		  `user_id` int(11) NOT NULL DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;";
		$db->sql_query($sql_data);
		
		$sql_data = "DROP TABLE IF EXISTS `" . $table_prefix . "gc_config`;";
		$db->sql_query($sql_data);
		
		$sql_data = "
		CREATE TABLE IF NOT EXISTS `" . $table_prefix . "gc_config` (
		  `config_name` varchar(255) CHARACTER SET latin1 NOT NULL,
		  `config_value` varchar(255) CHARACTER SET latin1 NOT NULL,
		  PRIMARY KEY (`config_name`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
		$db->sql_query($sql_data);
		
		$sql_data = "
		INSERT INTO `" . $table_prefix . "gc_config` (`config_name`, `config_value`) VALUES
		('active_text_color', 'FFFFFF'),
		('announcementbar_bg_color', 'D10000'),
		('announcementbar_text_color', 'FFFFFF'),
		('games_per_page', '10'),
		('game_console_online', '0'),
		('gc_panel_bgcolor', '646F73'),
		('gc_row_1', '444444'),
		('gc_row_2', '222222'),
		('gc_row_3', '6495ed'),
		('gc_table_border_color', '000000'),
		('headerbar_bg_color', '1083BC'),
		('headerbar_text_color', 'FFFFFF'),
		('hover_text_color', 'FFFFFF'),
		('link_text_color', 'FFFFFF'),
		('support_link', '$support_link'),
		('text_color', 'FFFFFF'),
		('visited_text_color', 'FFFFFF');";
		$db->sql_query($sql_data);
		
		$sql_data = "DROP TABLE IF EXISTS `" . $table_prefix . "gc_games`;";
		$db->sql_query($sql_data);

		$sql_data = "
		CREATE TABLE IF NOT EXISTS `" . $table_prefix . "gc_games` (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `game_name` varchar(100) CHARACTER SET latin1 NOT NULL,
		  `game_descript` text CHARACTER SET latin1,
		  `game_ver_major` int(11) NOT NULL DEFAULT '0',
		  `game_ver_minor` int(11) NOT NULL DEFAULT '0',
		  `game_ver_build` int(11) NOT NULL DEFAULT '0',
		  `game_round` int(11) NOT NULL DEFAULT '1',
		  `game_round_length` int(11) NOT NULL DEFAULT '0',
		  `game_ticks_left` int(11) NOT NULL DEFAULT '0',
		  `game_time_mgt` int(11) NOT NULL DEFAULT '0',
		  `game_members` int(11) NOT NULL DEFAULT '0',
		  `game_member_slots` int(11) NOT NULL DEFAULT '0',
		  `game_path` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
		  `game_bulletin` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
		  `game_bulletin_show` tinyint(4) NOT NULL DEFAULT '0',
		  `game_online` tinyint(4) NOT NULL DEFAULT '0',
		  `game_open` tinyint(4) NOT NULL DEFAULT '0',
		  `game_author` varchar(255) COLLATE utf8_bin NOT NULL,
		  `game_email` varchar(255) COLLATE utf8_bin NOT NULL,
		  `game_website` varchar(255) COLLATE utf8_bin DEFAULT NULL,
		  `game_credits` text COLLATE utf8_bin,
		  `game_history` text COLLATE utf8_bin,
		  `game_license` text COLLATE utf8_bin NOT NULL,
		  `game_copyrights` varchar(255) COLLATE utf8_bin NOT NULL,
		  `game_tables` text COLLATE utf8_bin,
		  `game_unique_id` text COLLATE utf8_bin NOT NULL,
		PRIMARY KEY (`id`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;";
		$db->sql_query($sql_data);

		set_config('gc_version_major', $major, TRUE);
		set_config('gc_version_minor', $minor, TRUE);
		set_config('gc_version_build', $build, TRUE);

		// Setup auths
		include($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
		$auth_admin = new auth_admin();
		$auth_admin->acl_add_option(array(
			'local'		=> array(),
			'global'	=> array(
				'u_gc',  
				'a_gc', 
				'a_gc_set_config',
				'a_gc_sty_config',
				'a_gc_u_config',
				'a_gc_a_config',
				'a_gc_d_config',
				'a_gc_m_config',
				
			),
		));
		
		include($phpbb_root_path . 'gc/eami.' . $phpEx);

		$eami = new eami();

		// Add our ACP module and a parent module...
		// Parent module...
		$module_data = array(
			'module_langname'	=> 'GAME_CONSOLE',
		);
		
		$eami->add_module('acp', 'ACP_CAT_DOT_MODS', $module_data);		
		
		// Main module...
		$sql_ary = array(
			'module_basename'	=> 'gameconsole',
			'module_langname'	=> 'GAME_CONSOLE_UPDATES',
			'module_mode'		=> 'update',
			'module_auth'		=> 'acl_a_gc_u_config');
			
		$eami->add_module('acp', 'GAME_CONSOLE', $sql_ary);

		$sql_ary = array(
			'module_basename'	=> 'gameconsole',
			'module_langname'	=> 'GAME_CONSOLE_SETTINGS',
			'module_mode'		=> 'settings',
			'module_auth'		=> 'acl_a_gc_set_config');
			
		$eami->add_module('acp', 'GAME_CONSOLE', $sql_ary);
		
		$sql_ary = array(
			'module_basename'	=> 'gameconsole',
			'module_langname'	=> 'GAME_CONSOLE_STYLE',
			'module_mode'		=> 'style',
			'module_auth'		=> 'acl_a_gc_sty_config');
			
		$eami->add_module('acp', 'GAME_CONSOLE', $sql_ary);
		
		$sql_ary = array(
			'module_basename'	=> 'gameconsole',
			'module_langname'	=> 'GAME_CONSOLE_ADD',
			'module_mode'		=> 'add',
			'module_auth'		=> 'acl_a_gc_a_config');
			
		$eami->add_module('acp', 'GAME_CONSOLE', $sql_ary);

		$sql_ary = array(
			'module_basename'	=> 'gameconsole',
			'module_langname'	=> 'GAME_CONSOLE_DELETE',
			'module_mode'		=> 'delete',
			'module_auth'		=> 'acl_a_gc_d_config');
			
		$eami->add_module('acp', 'GAME_CONSOLE', $sql_ary);
		
		$sql_ary = array(
			'module_basename'	=> 'gameconsole',
			'module_langname'	=> 'GAME_CONSOLE_MANAGE',
			'module_mode'		=> 'config',
			'module_auth'		=> 'acl_a_gc_m_config');
			
		$eami->add_module('acp', 'GAME_CONSOLE', $sql_ary);



		trigger_error($user->lang['INSTALL_WAS_SUCCESSFUL']);		
	}else{
		// This will redirect the founder back to the install & uninstall page while clearing the confirm key
		redirect(append_sid("{$phpbb_root_path}gc/index.$phpEx"));
	}	
}

if($cmd == $user->lang['INSTALL'])
{
	if($installed == TRUE)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['GAME_FOUND'] . '<br />';		
	}
	
	if ($user->data['user_type'] != USER_FOUNDER)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['UNAUTHORIZED_ACCESS'] . '<br />';
	}
		
	if(!$error)
	{
		$s_hidden_fields = build_hidden_fields(array(
			'cmd' => 'confirm_install',
			)
		);

		//Display confirm box
		confirm_box(false, $user->lang['CONFIRM_CONSOLE_INSTALL'], $s_hidden_fields);	
	}
}

if($error_msgs){
	$error_msgs = '<font color="#FF0000"><b>'.$error_msgs.'</b></font>';
}

// Lets build a page ...
$template->assign_vars(array(
	'ERROR_MSGS' 			=> $error_msgs,
	'INSTALLED_VERSION'		=> $version_installed,
	'VERSION_INSTALLING'	=> $installing_version,
	'S_INSTALLED'			=> $installed,
	)
);

page_header($p_title);

$template->set_filenames(array(
	'body' => 'mods/game_console/gc_index_body.html')
);
make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));

page_footer();
?>